/* *****************************************************************************************
 * 
 * *************************************************************************************** */


#include "app.h"
#include <uis.h>
#include <mma.h>
#include <synlib.h>
#include "../dbg/dbg.h"

// TODO: set previous state here
#define BACK_STATE			NEWAPP_STATE_MAIN

#define MAX_ENRTYES			6


// -----------------------------------------------------------------------------------------
// Local items enumerator

// TODO: edit this enum
enum {
	// static
	MENU_ENTRY_0 = 0,
	MENU_ENTRY_1,
	MENU_ENTRY_2,
	MENU_ENTRY_3,
	MENU_ENTRY_BACK,
	MENU_ENTRY_EXIT,
	
	MENU_MAX
};


// -----------------------------------------------------------------------------------------
// Local consts

static const MENU_ICON_T		icon_list[MENU_MAX] =
{
	{// MENU_ENTRY_0
	  0x1200006A,
	  0x12000069,
	  NULL,
	  LANG__NONE_
	},
	
	{// MENU_ENTRY_1
	  0x1200006A,
	  0x12000069,
	  NULL,
	  LANG__NONE_
	},
	
	{// MENU_ENTRY_2
	  0x1200006A,
	  0x12000069,
	  NULL,
	  LANG__NONE_
	},
	
	{
	  0x1200006A,
	  0x12000069,
	  NULL,
	  LANG__NONE_
	},
	
	{
	  0x1200006A,
	  0x12000069,
	  NULL,
	  LANG_BACK
	},
	
	{
	  0x1200006A,
	  0x12000069,
	  NULL,
	  LANG_EXIT
	}
};


// -----------------------------------------------------------------------------------------
// Local functions

static UINT32 HandleDone( EVENT_STACK_T * ev_st, void * app );
static UINT32 HandleSelect( EVENT_STACK_T * ev_st, void * app );


// -----------------------------------------------------------------------------------------
// Global handlers array

const EVENT_HANDLER_ENTRY_T ap_state_handlers[] =
{
	{ EV_DIALOG_DONE,			HandleDone },
   	{ EV_DONE,					HandleDone },		// Pressing "back" key
	{ EV_SELECT,				HandleSelect },		// Press "select" key

	{ STATE_HANDLERS_END,		NULL }
};


// -----------------------------------------------------------------------------------------
// Global enter/exit state functions

UINT32 APStateEnter( EVENT_STACK_T * ev_st, void * app, ENTER_STATE_TYPE_T type )
{
	NEWAPP_T *		papp = newapp(app);
	SU_PORT_T		port = papp->apt.port;
	UIS_DIALOG_T	dialog;
	MENU_ATTRIB_T	mattr;
	
	dbg("APStateEnter: Enter, type = %d", type);
	if ( type != ENTER_STATE_ENTER )
		return RESULT_OK;
	
	mattr.num = __min(MAX_ENRTYES, MENU_MAX);
	mattr.cursor = MENU_ENTRY_0 + 1;
	dialog = UIS_CreateIconicAppPickerTab( &port, NULL, (MENU_ICON_T *) icon_list, mattr, 0 );
	
	dbg("APStateEnter: UIS_CreateIconicAppPickerTab DONE, dialog = %d", dialog);
	
	if ( dialog == 0 )
		return RESULT_FAIL;
	papp->apt.dialog = dialog;
	
	papp->edited = FALSE;
	
	dbg("APStateEnter: END", 0);
	return RESULT_OK;
}

UINT32 APStateExit( EVENT_STACK_T * ev_st, void * app, EXIT_STATE_TYPE_T type )
{
	NEWAPP_T *		papp = newapp(app);
	UINT32			status;
	
	dbg("APStateExit: Enter, type = %d", type);
	if ( type != EXIT_STATE_EXIT )
		return RESULT_OK;
	
	status = APP_UtilUISDialogDelete( &papp->apt.dialog );
	
	dbg("APStateExit: END, status = %d", status);
	return status;
}


// -----------------------------------------------------------------------------------------
// Other local functions

UINT32 HandleDone( EVENT_STACK_T * ev_st, void * app )
{
	NEWAPP_T *		papp = newapp(app);
	UINT32			status;
	
	dbg("HandleDone: Enter", 0);
	APP_ConsumeEv(ev_st, app);
	
	// Pressing "back"/"done" key
	// TODO: add your code here
	if ( papp->edited ) {
		// TODO: add save to file code here
	}
	
	status = APP_UtilChangeState( BACK_STATE, ev_st, app );
	dbg("HandleDone: END, status = %d", status);
	return status;
}

UINT32 HandleSelect( EVENT_STACK_T * ev_st, void * app )
{
	NEWAPP_T *		papp = newapp(app);
	EVENT_T *		event = AFW_GetEv(ev_st);
	
	dbg("HandleSelect: Enter", 0);
	APP_ConsumeEv(ev_st, app);
	switch ( event->data.index - 1 ) {
		// TODO: add new list items here
		case MENU_ENTRY_0:
			dbg("HandleSelect: MENU_ENTRY_0", 0);
			break;
		case MENU_ENTRY_BACK:
			dbg("HandleSelect: MENU_ENTRY_BACK", 0);
			return APP_UtilChangeState( BACK_STATE, ev_st, app );
			break;
		case MENU_ENTRY_EXIT:
			dbg("HandleSelect: MENU_ENTRY_EXIT, call ELF_Exit", 0);
			return ELF_Exit(ev_st, app);
			break;
		default:
			warn("unknown list item %d", event->data.index - 1);
	}
	
	dbg("HandleSelect: END", 0);
	return RESULT_OK;
}

